
// loads open test that the user has not solved yet
function loadOpenTests() {
	
	new Ajax.Request( siteUrl + 'modules.php?action=loadOpenTests', {
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('centerArea').update( response.responseText ).show();
			}
		},
		onFailure: function( response ) {
			$('centerArea').update( 'Unknown error.' ).show();
		}
	});
}

function listQuestions(idkontrolltoo) {
	new Ajax.Request( siteUrl + 'tests.php',{
			parameters: {action: 'listQuestions', idkontrolltoo: idkontrolltoo},
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('centerArea').update( response.responseText ).show();
			}
		},
		onFailure: function( response ) {
			alert("Viga. Proovi mõne hetke pärast uuesti.");
		}
			});
	
}

function getTestData(idkontrolltoo) {
	//alert(idkontrolltoo);
	new Ajax.Request( siteUrl + 'tests.php',{
			parameters: {action: 'getTestData', idkontrolltoo: idkontrolltoo},
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('centerArea').update( response.responseText ).show();
			}
		},
		onFailure: function( response ) {
			alert("Viga. Proovi mõne hetke pärast uuesti.");
		}
			});
	
}

//Toggles open/closed state of test
function changeState(idkontrolltoo) {
	//alert(idkontrolltoo);
	new Ajax.Request( siteUrl + 'tests.php',{
			parameters: {action: 'changeState', idkontrolltoo: idkontrolltoo},
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				loadAllTests();
			}
		},
		onFailure: function( response ) {
			alert("Viga. Proovi uuesti.");
		}
			});
	
}


function getTestAdder() {
	new Ajax.Request( siteUrl + 'tests.php', {
		parameters: {action: "testadder"},
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('centerArea').update( response.responseText ).show();
			}
		},
		onFailure: function( response ) {
			$('centerArea').update( 'Unknown error.' ).show();
		}
	});
}

/**
 * Validates data coming from the form and creates insert a new test into the db.
 */
function addTest() {
	
	var kirjeldus = $('kirjeldus').value;
	var nimi = $('testName').value;
	var katseid = $('attempts').value;
	var avatud = $('open').value;
	
	if( kirjeldus == '' ) {
		alert("Sisesta kirjeldus");
		return;
	}

	if( nimi == '' ) {
		alert("Sisesta töö nimi");
		return;
	}

	new Ajax.Request( siteUrl + 'tests.php', {
		parameters: {action: 'add', katseid: katseid,
			kirjeldus: kirjeldus, avatud: avatud, nimi: nimi},
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				var text = response.responseText;
				if( text == 'OK' ) {
					loadAllTests();
				} else {
					alert("Viga: "+text);
				}
			}
		}
	});
}

function solve(testId) {
	var testId = testId;
	
	new Ajax.Request( siteUrl + 'modules.php?action=loadTest&testId='+testId, {
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('centerArea').update( response.responseText ).show();
			}
		},
		onFailure: function( response ) {
			alert("Failure");
		}
	});
}

/**
 * Fetches view for adding a question to a test
 */

function addQuestion(testId){
	var testId = testId;
	new Ajax.Request( siteUrl + 'modules.php?action=questionAdder&testId='+testId, {
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('centerArea').update( response.responseText ).show();
			}
		},
		onFailure: function( response ) {
			alert("Failure");
		}
	});
}


function alterTest(idkontrolltoo) {
	var kirjeldus = $('testDescription').value.trim();
	var nimi = $('testName').value.trim();
	
	
	new Ajax.Request( siteUrl + 'tests.php',{
			parameters: {action: 'alterTest', idkontrolltoo: idkontrolltoo,
				description: kirjeldus, name: nimi},
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				if(response.responseText == "OK"){
					alert("Muudatused tehtud");
				} else
				alert(response.responseText);
			}
		},
		onFailure: function( response ) {
			alert("Viga. Proovi uuesti.");
		}
			});
	
}

function deleteTest(idkontrolltoo) {
	
	var r=confirm("Kustutan töö?");
	if(r){
		new Ajax.Request( siteUrl + 'tests.php',{
			parameters: {action: 'deleteTest', idkontrolltoo: idkontrolltoo},
		onSuccess: function( response ) {
			var text = response.responseText;
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				if(text=="OK"){
					loadAllTests();
				} else alert("Viga. Proovi hiljem uuesti.");
				
			}
		},
		onFailure: function( response ) {
			alert("Viga. Proovi uuesti.");
		}
			});
		
	}

	
}

function loadAllTests(){
	new Ajax.Request( siteUrl + 'modules.php?action=loadAllTests', {
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('centerArea').update( response.responseText);
			}
		},
		onFailure: function( response ) {
			$('centerArea').update( 'Unknown error.' ).show();
		}
	});	
}



